﻿using System.Data;
using System.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Home.Haushalt.Lib.Element
{
    /// <summary>
    /// 
    /// </summary>
    [ElementExtension("Element", 4)]
    public class Date : ElementBase
    {
        #region CONSTANTS
        /// <summary></summary>
        public new static readonly Type ElementType = typeof(Date);

        /// <summary></summary>
        public class Fields
        {
            public const string Id = ELEMENTPROPERTY_ID;
            public const string Label = ELEMENTPROPERTY_LABEL;
            public const string Description = ELEMENTPROPERTY_NOTE;
            public const string Name = "n";
            public const string Summe = "s";
            public const string Active = "a";
            public const string TerminTag = "x";
            public const string IsFree = "f";
            public const string Posting = "z";
            public const string PostenId = "p";
            public const string OpponentId = "o";
            public const string SaldotypId = "y";
            public const string GroupId = "g";
            public const string Termin = "t";

            public const string Saldo = "saldo";
        }
        #endregion CONSTANTS

        /// <summary></summary>
        public override string PropertyFormCaption { get { return (Row != null) ? Row[Fields.Name].ToString() : ""; } }

        /// <summary>
        /// 
        /// </summary>
        public Date() : base() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentDataRow"></param>
        public Date(DataRow parentDataRow) : base(parentDataRow) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="savableElementTable"></param>
        /// <param name="sourceElementTable"></param>
        internal override void FillSavableData(DataTable savableElementTable, DataTable sourceElementTable = null)
        {
            //Calculate-Spalte löschen
            sourceElementTable = DAO.Tables[typeof(Date)].Copy();
            sourceElementTable.Columns.Remove(Fields.Saldo);

            //Füllen
            base.FillSavableData(savableElementTable, sourceElementTable);
        }

        /// <summary>
        /// 
        /// </summary>
        internal override void AfterOpenElementTable(DataTable elementTable)
        {
            //Calculate-Spalte hinzufügen
            elementTable.Columns.Add(Fields.Saldo, typeof(double));
        }

        ///// <summary>
        ///// 
        ///// </summary>
        //public override void PreCalculateUnboundFields()
        //{
        //    base.PreCalculateUnboundFields();

        //    if (MonthbalanceController.Balances.Count > 0)
        //    {
        //        foreach (MonthbalanceController mbc in MonthbalanceController.Balances.Values)
        //        {
        //            mbc.CalculateScheduledSaldoTempField = Double.NaN;
        //        }

        //        MonthbalanceController preBalance = MonthbalanceController.Balances.Values[0];
        //        preBalance.CalculateScheduledSaldoTempField = preBalance.StartSaldo;
        //    }
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public override Dictionary<string, object> CalculateUnboundFields()
        //{
        //    Dictionary<string, object> result = new Dictionary<string, object>();

        //    if (CheckColumnsInRow(Fields.Summe, Fields.SaldotypId, Fields.Posting, Fields.Active))
        //    {
        //        string termin = Row[Fields.Posting].ToDateTime().Year.ToString() + Row[Fields.Posting].ToDateTime().Month.ToString("00");

        //        if (MonthbalanceController.Balances.ContainsKey(termin))
        //        {
        //            MonthbalanceController bindedBalance = MonthbalanceController.Balances[termin];

        //            if (Row[Fields.Active].ToBool(false))
        //            {
        //                if (bindedBalance.CalculateScheduledSaldoTempField.Equals(Double.NaN))
        //                {
        //                    MonthbalanceController preBalance = MonthbalanceController.Balances.ElementAt(MonthbalanceController.Balances.IndexOfValue(bindedBalance) - 1).Value;
        //                    bindedBalance.CalculateScheduledSaldoTempField = preBalance.CalculateScheduledSaldoTempField;
        //                }

        //                DateTime posting = Row[Fields.Posting].ToDateTime();

        //                if (posting >= bindedBalance.BalanceFrom && posting <= bindedBalance.BalanceTo)
        //                {
        //                    double dateSumme = Row[Fields.Summe].ToDouble() * Row[Fields.SaldotypId].ToDouble();

        //                    bindedBalance.CalculateScheduledSaldoTempField += dateSumme;
        //                }
        //            }

        //            result.Add(Fields.Saldo, bindedBalance.CalculateScheduledSaldoTempField);
        //        }
        //    }

        //    return result;
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ElementBase New()
        {
            base.New();

            Row[Fields.Posting] = DateTime.Now;

            if (ParentElement is Posten)
            {
                Row[Fields.PostenId] = ParentElement.Row[Posten.Fields.Id];
                Row[Fields.OpponentId] = ParentElement.Row[Posten.Fields.OpponentId];
                Row[Fields.Summe] = ParentElement.Row[Posten.Fields.Summe];
                Row[Fields.GroupId] = ParentElement.Row[Posten.Fields.GroupId];
                Row[Fields.Name] = ParentElement.Row[Posten.Fields.Name];
                Row[Fields.SaldotypId] = ParentElement.Row[Posten.Fields.SaldotypeId];
                Row[Fields.Posting] = ParentElement.Row[Posten.Fields.DateFrom];
            }
            else if (ParentElement is Opponent)
            {
                Row[Fields.OpponentId] = ParentElement.Row[Opponent.Fields.Id];
            }

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override Color GetRowForeColor()
        {
            Color foreColor = base.GetRowForeColor();

            if (CheckColumnsInRow(Fields.SaldotypId))
            {
                if (Row[Fields.SaldotypId].ToInt() >= 0)
                    foreColor = DAO.Settings.Selected.RowStateColorPlus;
                if (Row[Fields.SaldotypId].ToInt() < 0)
                    foreColor = DAO.Settings.Selected.RowStateColorMinus;
            }

            return foreColor;
        }

    }
}
